home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / mtl100je.zip / EXAMPLE3.CPP < prev    next >
C/C++ Source or Header  |  1993-05-06  |  2KB  |  65 lines

  1. //--------------------------------------------------------------------------
  2. //
  3. //      EXAMPLE3.CPP: example for DOS multithreading library.
  4. //      Copyright (c) J.English 1993.
  5. //      Author's address: je@unix.brighton.ac.uk
  6. //
  7. //      Permission is granted to use copy and distribute the
  8. //      information contained in this file provided that this
  9. //      copyright notice is retained intact and that any software
  10. //      or other document incorporating this file or parts thereof
  11. //      makes the source code for the library of which this file
  12. //      is a part freely available.
  13. //
  14. //--------------------------------------------------------------------------
  15. //
  16. //      This example starts 3 identical threads, each of which repeatedly
  17. //      displays a message on the same line.  1-second timeslices are used.
  18. //
  19. //--------------------------------------------------------------------------
  20.  
  21. #include <stdio.h>
  22. #include <conio.h>
  23. #include "threads.h"
  24.  
  25. class Example3 : public DOSThread
  26. {
  27.   public:
  28.     Example3 (int n)      { num = n; }
  29.  
  30.   protected:
  31.     virtual void main ();
  32.  
  33.   private:
  34.     int num;
  35. };
  36.  
  37. void Example3::main ()
  38. {
  39.     char c [70];
  40.     sprintf (c, "Starting thread %d\n", num);
  41.     fputs (c, stdout);
  42.     sprintf (c, "Thread %d\r", num);
  43.     while (!userbreak())
  44.         fputs (c, stdout);
  45.     sprintf (c, "\nEnd of thread %d\n", num);
  46.     fputs (c, stdout);
  47. }
  48.  
  49. void main ()
  50. {
  51.     DOSThread::timeslice (18);
  52.     Example3 e1 (1), e2 (2), e3 (3);
  53.  
  54.     puts ("Press CONTROL-BREAK to terminate");
  55.     puts ("Press any key to start...");
  56.     getch ();
  57.  
  58.     if (e1.run ())
  59.         puts ("Thread 1 started");
  60.     if (e2.run ())
  61.         puts ("Thread 2 started");
  62.     if (e3.run ())
  63.         puts ("Thread 3 started");
  64. }
  65.